package jp.jig.hackathon;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class Register extends HttpServlet {
	public static Map<String, String> id;
	
	@Override
	public void init() throws javax.servlet.ServletException {
		Register.id = new HashMap<String, String>();
	};

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) 
	throws IOException {
		resp.setContentType("text/html");
		resp.setStatus(HttpServletResponse.SC_OK);
		PrintWriter pw = resp.getWriter();
		pw.println("<form action=\"registration\" method=\"post\">");
		pw.println("<span>user: </span>");
		pw.println("<input type=\"text\" name=\"user\" />");
		pw.println("<span>RegistrationID: </span>");
		pw.println("<input type=\"text\" name=\"rid\" />");
		pw.println("<input type=\"submit\" value=\"regist\"/>");
		pw.println("</form>");
		pw.close();
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
		String user = req.getParameter("user");
		String rid = req.getParameter("rid");
		System.out.println("(registration) date: " + new Date());
		System.out.println("(registration) user: " + user);
		System.out.println("(registration) rid: " + rid);
	
		PrintWriter pw = resp.getWriter();
		pw.println("<h1>regstration</h1>");
		pw.println("user: " + user);
		pw.println("rid: " + rid);
		pw.close();
		
		Register.id.put(user, rid);
	}
}
